This application is based on an application No. 11-259542 filed in Japan, the content of which is hereby incorporated by reference.
1. Field of the Invention
The present invention relates to a program conversion apparatus for converting a source program written in an object-orientated language into an executable program.
2. Description of the Prior Art
The use of object-orientated languages such as C++ and Java (Java is a trademark of Sun Microsystems) in recent software development has caused program development to proceed at an increasingly higher pace.
Such object-orientated languages introduce the concept of a xe2x80x98classxe2x80x99, an abstract data type that binds together a data structure, made up of a set of variables, with a set of routines, performing processing using the data expressed by the data structure. In C++, the data structure is known as a xe2x80x98data memberxe2x80x99 and the routine as a xe2x80x98member functionxe2x80x99.
Files 901 and 902 in FIG. 23 show examples of class definitions based on the C++ standard. In the drawing, file 901 includes class definitions 911 and 912 and file 902 include a class definition 913.
The class definition 911 in file 901 includes lines 920 to 924. Line 920 declares the start of the class definition 911 and indicates that its class name is X. Line 921 shows a declaration of a data member, and indicates that class X has an int type member x as a data member. Lines 922 to 924 are declarations of member functions. Line 922 shows that class X has a member function f that has no argument and does not return a return value. Line 923 shows that class X has a function g that has an int type argument and returns an int type return value. Line 924 shows that class X has a function h that has an int type argument and does not return a return value.
The keyword virtual written at the start of the declarations for functions g and h in lines 923 and 924, is a keyword used in C++. This keyword indicates that the relevant member function is a special function known as a xe2x80x98virtual functionxe2x80x99.
FIG. 24 shows the inheritance relationship between the three classes X, Y and Z shown in FIG. 23. As shown in the drawing, class X is inherited by class Y, and class Y by class Z. Here, class X is described as the base class of class Y, and class Y as a derived class of class X. Similarly, class Y is the base class of class Z, and class Z is a derived class of class Y. Furthermore, although class X is not directly inherited by class Z, it is indirectly inherited via class Y. Thus, class X may be referred to as an indirect base class of class Z and class Z as an indirect derived class of class X.
In languages such as C++ and Java, a pointer to a base class is assigned to an address of an object of the base class, so that the pointer to the base class indicates the object of the base class. Furthermore, the pointer to the base class is assigned to an address of an object of a derived class, so that the pointer to the base class can also indicate a derived class object. For example, the pointer to class X can indicate the object of class X, the object of class Y and the object of class Z. Similarly, the pointer to class Y can indicate the object of class Y and also the object of class Z.
In FIG. 25, line 951 shows that an object indicated by a pointer ptr_x belongs to one of the classes X, Y and Z, line 952 shows that an object indicated by a pointer ptr_y belongs to one of the classes Y and Z, and line 953 shows that an object indicated by a pointer ptr_z belongs to the class Z.
Here, a called function dynamically determined at execution according to the type of object shown by the pointer is referred to as a virtual function. An example of coding used when a function to be executed is dynamically determined by the object indicated by the pointer is shown in FIG. 26.
In the drawing, line 961 shows that a function g is called via the pointer ptr_x to class X. When the pointer ptr_x indicates the object for class X, a function g in class X is executed, and when it indicates the object for class Z, a function g in class Z is executed. Line 962 shows that a function g is called via the pointer ptr_y to class Y. When the pointer ptr_y indicates the object for class Y, a function g in class Y is executed, and when it indicates the object for class Z, a function g in class Z is executed. Line 963 shows that a function g is called via the pointer ptr_z to class Z, and executes a function g in class Z.
A declaration of a virtual function in a derived class, as in FIG. 23, lines 931 and 941, must be performed using the same function name, argument type and return type as the virtual function declared in the base class.
In order to express this kind of special function call, a virtual function table is generated for a class when a virtual function is declared in that class. The virtual function table stores the addresses of all the virtual functions declared in the class. Furthermore, an object for a class in which a virtual function is declared holds a pointer to the virtual function table as one of its members. The pointer to the virtual function table is generated implicitly by a compiler.
FIG. 27 shows examples of objects for classes declaring a virtual functions and structures for virtual function tables. In an object obj_x 701 shown in FIG. 27A, an area 702 shown by a start address andobj_x stores a member x, and an area 703 shown by a next address andobj_x+4 stores a pointer to a virtual function table for class X. Furthermore, an area 712 shown by the pointer to the virtual function table for class X, is the start of a virtual function table 711 for class X. A start address of the virtual function table 711, in other words an area 712 shown by _vtb1_X, stores an address of a virtual function g in class X. An area 713 shown by a next address _vtb1_X+4 stores an address of a virtual function h in class x.
Furthermore, FIG. 27B similarly shows an object obje_y 721 of class Y, structured from an area 722 storing a member x, an area 723 storing a pointer to a virtual function table for class Y, and an area 724 storing a member y. A virtual function table 731 for class Y is structured from an area 732 storing an address of a virtual function g in class Y, and an area 733 storing an address of a virtual function h in class Y.
FIG. 27C similarly shows an object obj_z 741 for class Z, structured from an area 742 storing a member x, an area 743 storing a pointer to a virtual function table for class Z, an area 744 storing a member y, and an area 745 storing a member z. A virtual function table 751 for class Z is structured from an area 752 storing an address of a virtual function g in class Z, and an area 753 storing an address of a virtual function h in class Z.
Calling virtual functions is performed in the following way.
First, a pointer to a virtual function table in an object is obtained. Next, a pointer to a call function is obtained from the virtual function table. Finally, the function is called indirectly using the pointer to the function.
If the pointer to class X indicates the object for class X when this processing is performed, a virtual function in class X is called, since the pointer to the virtual function table indicates a virtual function table for class X. If the pointer indicates the object for class Y, the pointer to the virtual function table indicates a virtual function table for class Y, so a virtual function in class Y is called. If, however, the pointer indicates the object for class Z, the pointer to the virtual function table indicates a virtual function table for class Z, so a virtual function in class Z is called.
FIG. 28A shows a source program 781 for calling a virtual function using C++, and FIG. 28B shows an executable program 791 generated based on the source program 781. In the drawing, line 782 shows a call for a function g via a pointer ptr_z to class Z. Line 792 is an instruction for obtaining a pointer to a virtual function table inside an object. Line 793 is an instruction for obtaining a pointer to a call function from the virtual function table. Line 794 is an instruction for setting an argument value for the function. Line 795 is an instruction for calling the function.
On the other hand, suppose that the function written in the source program 781 shown in FIG. 28A is not a virtual function. The resulting executable program generated based on the source program 781 in this case is shown in FIG. 28C. In the drawing, line 772 is an instruction for setting an argument value for a function, and line 773 is an instruction for calling the function.
However, when a virtual function call is converted to an executable program, the number of executable instructions generated is greater than when a function that is not virtual is called. This means that the time taken to execute the virtual function call may be longer.
The object of the present invention is to overcome the above problem by providing a program conversion apparatus that reduces the number of execution instructions generated when converting into an executable program virtual function calls present in special circumstances in a program written in object-orientated language.
In order to achieve the above object, the present invention may be a program conversion apparatus that generates at least one executable instruction from a source program written in an object-orientated programming language. This program conversion apparatus may include the following. A storage unit stores the source program including a plurality of class definitions and a call instruction having a function name, each class definition defining a class, an inheritance relationship between the class and another class, and a virtual function belonging to the class and identified by the function name. Here, only one of the virtual functions defined in the plurality of class definitions is executed when the call instruction is executed. A judging unit judges whether a single virtual function from the virtual functions can be determined as the virtual function identified by the function name in the call instruction, judgement performed by referring to the source program without executing the call instruction. A generating unit generates, when a single function is determined, an executable instruction for calling directly an executable instruction group corresponding to the single virtual function.
In the above construction, if a single virtual function identified by the function name in the call instruction can be determined, an executable instruction group for calling the virtual function directly is generated. This enables the number of executed executable instructions to be reduced, and increases the generating efficiency of the executable program. Here, the judging unit may judge whether a single virtual function can be determined by using the class definitions to judge whether a virtual function identified by the function name in the call instruction belongs to a class that is not inherited by another class, and the generating unit generates the executable instruction when the virtual function belongs to a class that is not inherited by another class.
The above construction judges whether a single virtual function can be determined, by using class definitions to judge whether a virtual function identified by the function name in the call instruction belongs to a class that is not inherited by another class. This ensures that judgement can be performed accurately.
Here, when a class inherits another class, a class definition in the source program stored in the storage means may include the other class, the other class being a base class. Furthermore, the judging unit may include the following. A class extracting unit reads all class definitions included in the source program, extracts classes from the read class definitions, and stores the extracted classes. An inheritance relationship extracting unit reads all class definitions included in the source program, extracts base classes from the read class definitions, and stores the extracted base classes. An inheritance relationship analyzing unit extracts classes from the classes stored in the class extracting means, excluding the base classes stored in the inheritance relationship extracting unit, and stores the extracted classes. An optimization information attaching unit attaches optimization information to the source program stored by the storage unit when the source program includes a class definition including a class stored in the inheritance relationship analyzing unit. Then, a class judging unit judges, using the optimization information attached to the source program, whether a virtual function identified by the function name in the call instruction belongs to a class that is not inherited by another class.
The above construction attaches optimization information indicating that a class is not inherited by another class to the source program including class definitions. This means that if the source program is reused, the process for attaching optimization information can be omitted.
Here, the optimization information attaching means attaches optimization information to the source program so as to correspond to class definitions that define classes stored by the inheritance relationship analyzing unit.
The above construction attaches optimization information so as to correspond to class definitions including classes stored in the inheritance relationship analyzing unit, enabling such class definitions to be easily extracted.
Here, the optimization information attaching means attaches optimization information to the source program so as to correspond to virtual functions included in class definitions that define classes stored in the inheritance relationship analyzing unit.
The above construction attaches optimization information so as to correspond to virtual functions in class definitions including classes stored in the inheritance relationship analyzing unit, enabling such virtual functions to be easily extracted.
Here, the optimization information is a keyword used in an object-orientated programming language.
In the above construction, the optimization information is a keyword used in an object-orientated programming language, enabling the program to retain its universality, and the optimization information to be recognized by all compilers that can translate the keyword.
Here, the optimization information is a character string unique to the program conversion apparatus.
In the above construction, only a particular compiler apparatus can recognize the optimization information, and other compilers will ignore it, enabling the optimization information for that particular compiler to be held and the authenticity of the program to be maintained.
Here, the program conversion apparatus generates an executable program from the source program by first producing an intermediate program. When a class defined by a class definition in the source program inherits another class, the class definition includes a base class showing the other class. Furthermore, the judging unit includes the following. A class extracting unit reads all class definitions included in the source program, and extracts classes from the read class definitions. An inheritance relationship extracting unit reads all class definitions included in the source program, and extracts base classes from the read class definitions. An inheritance relationship analyzing unit extracts classes from the classes extracted by the class extracting unit, excluding the base classes extracted by the inheritance relationship extracting unit. An intermediate generating unit generates, from the call instruction included in the source program, an intermediate call instruction having a function name. An intermediate judging unit judges whether the virtual function called by the generated intermediate call instruction belongs to a class extracted by the inheritance relationship analyzing unit. Here, the generating unit generates the executable instruction when the virtual function is judged to belong to an extracted class.
The above construction can judge whether a virtual function identified by the function name in the call instruction belongs to a class that is not inherited by another class, without altering the source program, making unified management of the source program easier.
Here, the judging unit judges whether a single virtual function can be determined by using a class definition included in the source program to judge whether a virtual function identified by the function name in the call instruction is overridden. The generating unit generates the executable instruction when the judging means judges that the virtual function is not overridden.
This construction judges whether a single virtual function can be determined by using a class definition included in the source program to judge whether a virtual function identified by the function name in the call instruction is overridden. This ensures that judgement is performed accurately.
Here, when a class defined by a class definition in the source program inherits another class, the class definition includes a base class showing the other class. Furthermore, the judging unit includes an overridden function extracting unit, an optimization information attaching unit, an overridden judging unit and the generating unit. The overridden function extracting unit reads all class definitions included in the source program, extracts overridden virtual functions by using the read class definitions, and includes the following. A reading/judging unit reads a class definition included in the source program, and judges whether the read class definition includes a base class. A first determining unit determines, when a base class is not included in the read class definition, that a virtual function included in the read class definition belongs to the class defined by the read class definition. A second determining unit provisionally determines, when a base class is included in the read class definition, that a virtual function determined to belong to the base class belongs to the class defined in the read class definition. Then, the second determining unit judges whether a virtual function included in the read class definition is identical to the provisionally determined virtual function. If the virtual functions are identical, the second determining unit determines that the virtual function included in the read class definition is to override the provisionally determined virtual function, and extracts the provisionally determined virtual function as an overridden function. Following this, the optimization information attaching unit attaches optimization information showing an extracted overridden function to the source program. Then, the overridden judging unit judges whether an virtual function identified by the function name in the call instruction is an overridden function, using the optimization information attached to the source program, and the generating unit generates the executable instruction when the overridden judging unit judges that the virtual function is an overridden function.
In the above construction, optimization information showing overridden virtual functions is attached to the source program, enabling processing for attaching optimization information to be omitted if such a source program is reused.
Here, the program conversion apparatus generates an executable program from the source program via an intermediate program, each class definition in the source program stored in the storage means including, when a defined class inherits another class, a base class showing another class. Furthermore, the judging unit includes an overridden function extracting unit, an optimization information attaching unit, an intermediate extracting unit, an intermediate judging unit and the generating unit. The overridden function extracting unit reads all class definitions included in the source program, extracts overridden virtual functions by using the read class definitions, and includes the following. A reading/judging unit reads a class definition included in the source program, and judges whether the read class definition includes a base class. A first determining unit determines, when a base class is not included in the read class definition, that a virtual function included in the read class definition belongs to the class defined by the read class definition. A second determining unit provisionally determines, when a base,class is included in the read class definition, that a virtual function determined to belong to the base class belongs to the class defined in the read class definition. Then, the second determining unit judges whether a virtual function included in the read class definition is identical to the provisionally determined virtual function. If the virtual functions are identical, the second determining unit determines that the virtual function included in the read class definition is to override the provisionally determined virtual function, and extracts the provisionally determined virtual function as an overridden function. The intermediate generating unit generates, from the call instruction included in the source program, an intermediate call instruction having a function name. Then, the intermediate judging unit judges whether a virtual function identified by the function name in the generated intermediate call instruction is an overridden function. If the virtual function is judged to be an overridden function, the generating unit generates the executable instruction.
The above construction can judge whether a virtual function identified by the function name in the call instruction is an overridden function without altering the source program, making unified management of the source program easier.